package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class LoginPage extends Utils {
    public static String url = "http://8.156.68.126:8080/login.html";

    public LoginPage() {
        super(url);
    }

    //检查页面是否可以正常打开
    public void checkPageRight() throws InterruptedException {
        //等待弹幕加载完毕
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".toast.Andu")));
        Thread.sleep(300);

        //检查模态窗元素是否加载成功
        driver.findElement(By.cssSelector("body > div.login-container"));

        //检查登录框
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#submit"));
    }

    //异常登录
    public void LoginFail() {
        //1.密码错误
        driver.findElement(By.cssSelector("#username")).sendKeys("Rubens");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();

        // 定位弹幕（通过类名和文本内容）
        WebElement Toast01 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".toast.error")));

        // 验证弹窗内容
        assert Toast01.getText().equals("登录失败");

        //先清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        //2.用户不存在

        // 关键点：等待第一个弹窗完全消失（通过元素不可见或移除）
        wait.until(ExpectedConditions.invisibilityOf(Toast01));

        driver.findElement(By.cssSelector("#username")).sendKeys("Rubenddddddddd");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();

        // 定位弹幕（通过类名和文本内容）
        WebElement Toast02 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".toast.error")));

        // 验证弹窗内容
        assert Toast02.getText().equals("登录失败");
    }

    //成功登录
    public void LoginSuc() throws InterruptedException {
        //先清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("Rubens");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();

        //登陆成功会进入到大厅---h1
        wait.until(ExpectedConditions.urlContains("game_hall"));
        assert driver.getCurrentUrl().contains("game_hall");
        Thread.sleep(500);
    }
}
